const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin');
const DotEnv = require('dotenv-webpack');

module.exports = {
    mode: process.env.NODE_ENV,
    stats: {
        children: true,
        errorDetails: true
    },
    devServer: {
        static: [
            {
                directory: path.join(__dirname, 'sdk/audio/ring_data'),
            },
            {
                directory: path.join(__dirname, 'sdk/audio/wasm'),
            },
        ],
        https: true
    },

    // optimization: {
    //     runtimeChunk: "single"
    // },

    target: ['web', 'es2020'],

    devtool: "source-map",

    entry: {
        wlsdk: {
            import: "./sdk/wlsdk.ts",
            library: {
                name: "wlsdk",
                type: "umd"
            }
        },

        wlsdk_cfg: {
            import: "./sdk/wlsdk_config.ts",
            filename: "./wlsdk_config.js"
        }
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        //path: path.resolve(__dirname, "dist"),
        globalObject: "this",
        publicPath: "/",
        assetModuleFilename: 'assets/[name][contenthash][ext]',
        clean: true
    },
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                loader: 'ts-loader',
                options: {
                    compilerOptions: {
                        outDir: path.resolve(__dirname, 'dist')
                    }
                },
                exclude: /node_modules/
            },
            {
                test: /\.(js|jsx)$/,
                include: [path.resolve(__dirname, 'sdk/**')],
                loader: "babel-loader"
            },
            {
                test: /\.(wav|wasm)$/,
                type: "asset/resource"
            }
        ]
    },

    resolve: {
        extensions: ['.tsx', '.ts', '.js'],
        // 需要配合tsconfig.json一起，否则这个用在typescript不管用
        alias: {
            "@": path.resolve(__dirname, './sdk')
        }
    },

    experiments: {
        asyncWebAssembly: true
    },

    plugins: [
        new HtmlWebpackPlugin({
            title: "Webpack App",
            template: "./index.html"
        }),

        new DotEnv({
            path: `./${process.env.NODE_ENV}.env`,
            expand: true,
            prefix: 'import.meta.env.'
        })
    ]
}
